﻿#define _CRT_SECURE_NO_WARNINGS


//知识点：单调栈或ST表 + ⼆分
//题意为求数列的所有连续非空⼦序列的极差之和，极差等于最大值减最小值，所以题目又可以转换为求所有连续非空
//⼦序列的最⼤值之和减所有连续⾮空⼦序列的最小值之和。


#include <iostream>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int n;
ll a[N], l[N], r[N], stk[N], tt;
ll calc(int type)
{
	tt = 0;
	for (int i = 1; i <= n; i++) 
		l[i] = 1, r[i] = n;
	for (int i = 1; i <= n; i++) 
	{
		while (type && tt && a[stk[tt]] < a[i]) 
			r[stk[tt--]] = i - 1;
		while (!type && tt && a[stk[tt]] > a[i]) 
			r[stk[tt--]] = i - 1;
		l[i] = stk[tt] + 1;
		stk[++tt] = i;
	}
	ll res = 0;
	for (int i = 1; i <= n; i++) 
		res = (res + (i - l[i] + 1) * (r[i] - i + 1) % mod *
		a[i] % mod) % mod;
	return res;
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i) 
		cin >> a[i];
	cout << (calc(1) - calc(0) + mod) % mod;
	return 0;
}



